﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Web.UI;
using BLL;
using Model;
using Wuqi.Webdiyer;

namespace WebApplication1.InfoManagement
{
    public partial class Tool_CodeManagement : System.Web.UI.Page
    {
        private string var_Work_Shop
        {
            get { return ViewState["Work_Shop"].ToString(); }
            set { ViewState["Work_Shop"] = value; }
        }
        private string var_Tool_Code
        {
            get { return ViewState["Tool_Code"].ToString(); }
            set { ViewState["Tool_Code"] = value; }
        }
        private string var_Tool_Type_Spec
        {
            get { return ViewState["Tool_Type_Spec"].ToString(); }
            set { ViewState["Tool_Type_Spec"] = value; }
        }

        private Tool_Code tc;
        private Config c;
        private string message;
        private Tool_CodeInfo TcInfo;

        public Tool_CodeManagement()
        {
            tc = new Tool_Code();
            c = new Config();
            TcInfo =new Tool_CodeInfo(); 
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindWork_Shop();
                var_Work_Shop = "";
                var_Tool_Code = "";
                var_Tool_Type_Spec = "";
                BindTool_Code();
            } 
            Delall.Attributes.Add("onclick", "return confirm('你确认要删除这批数据!')"); 
        }

        private void BindWork_Shop()
        {
            this.DropDownList1.DataSource = c.Config_GetAll_forSingleType(enum_configType.WORK_SHOP);
            this.DropDownList1.DataTextField = "ItemName";
            this.DropDownList1.DataValueField = "ItemName";
            this.DropDownList1.DataBind();
            System.Web.UI.WebControls.ListItem li = new System.Web.UI.WebControls.ListItem("--请选择--", "");
            this.DropDownList1.Items.Insert(0, li);
        }

        private void BindTool_Code()
        {
            Tool_CodeInfo tci = new Tool_CodeInfo
            {
                Work_Shop = string.IsNullOrEmpty(var_Work_Shop) ? null : var_Work_Shop,
                Tool_Code = string.IsNullOrEmpty(var_Tool_Code) ? null : var_Tool_Code,
                Tool_Type_Spec = string.IsNullOrEmpty(var_Tool_Type_Spec) ? null : var_Tool_Type_Spec,
            };
            IList<Tool_CodeInfo> list = tc.Tool_Code_GetbyMultiFact(tci);
            this.Lbl_RecordsCount.Text = list.Count.ToString();
            //this.GridView1.DataSource = list;
            //this.GridView1.DataBind(); 
         
            //初始化分页数据源实例
            PagedDataSource pds = new PagedDataSource();
            //设置总行数
            AspNetPager1.RecordCount = list.Count;
            //设置分页的数据源
            pds.DataSource = list;
            //设置当前页
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            //设置每页显示页数
            pds.PageSize = AspNetPager1.PageSize;
            //启用分页
            pds.AllowPaging = true;
            //设置GridView的数据源为分页数据源
            GridView1.DataSource = pds;
            //绑定GridView
            GridView1.DataBind();
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            //调用绑定分页和GridView
            AspNetPager1.CurrentPageIndex = (sender as AspNetPager).CurrentPageIndex;
            BindTool_Code();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var_Work_Shop = this.DropDownList1.SelectedValue;
            var_Tool_Code = this.TextBox1.Text.Trim();
            var_Tool_Type_Spec = this.TextBox2.Text.Trim();
            BindTool_Code();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            BindTool_Code();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.Delall.Visible = true;
            GridView1.EditIndex = e.NewEditIndex;
            BindTool_Code();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.Delall.Visible = true;
            GridView1.EditIndex = -1;
            BindTool_Code();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Tool_CodeInfo tci = new Tool_CodeInfo();
            tci.ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
            tci.Work_Shop = ((DropDownList)(GridView1.Rows[e.RowIndex].FindControl("DDLWork_Shop"))).SelectedValue;
            tci.Tool_Code = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim().ToString();
            tci.Tool_Type_Spec = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim().ToString();

            tc.Update(tci);
            GridView1.EditIndex = -1;
            BindTool_Code();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            this.Delall.Visible = true;
            int ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
            tc.Delete(ID);
            BindTool_Code();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
                }
            }
            if (((DropDownList)e.Row.FindControl("DDLWork_Shop")) != null)
            {
                DropDownList ddlDepartment = (DropDownList)e.Row.FindControl("DDLWork_Shop");
                ddlDepartment.DataSource = c.Config_GetAll_forSingleType(enum_configType.WORK_SHOP);
                ddlDepartment.DataTextField = "ItemName";
                ddlDepartment.DataValueField = "ItemName";
                ddlDepartment.DataBind();
                ddlDepartment.SelectedValue = ((HiddenField)e.Row.FindControl("HDFWork_Shop")).Value.Trim();
            }
            if (((DetailsView)e.Row.FindControl("DetailsView1")) != null)
            {
                DetailsView d = (DetailsView)e.Row.FindControl("DetailsView1");
                if (((DropDownList)d.Rows[2].Cells[1].FindControl("DDLWork_Shop")) != null)
                {
                    DropDownList ddlDepartment1 = (DropDownList)d.Rows[0].Cells[1].FindControl("DDLWork_Shop");
                    ddlDepartment1.DataSource = c.Config_GetAll_forSingleType(enum_configType.WORK_SHOP);
                    ddlDepartment1.DataTextField = "ItemName";
                    ddlDepartment1.DataValueField = "ItemName";
                    ddlDepartment1.DataBind();
                }
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            this.Delall.Visible = false;
            if (e.CommandName == "Insert")
            {
                GridView1.DataSource = null;
                GridView1.DataBind();
            }
        }

        protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
        {
            this.Delall.Visible = true;
            BindTool_Code();
        }

        protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            this.Delall.Visible = true;
            DetailsView d = (DetailsView)this.GridView1.Controls[0].Controls[0].FindControl("DetailsView1");
            Tool_CodeInfo tci = new Tool_CodeInfo();
            tci.Work_Shop = ((DropDownList)(d.Rows[0].Cells[1].FindControl("DDLWork_Shop"))).SelectedValue;
            string ToolCode = ((TextBox)(d.Rows[1].Cells[1].Controls[0])).Text.Trim(); 
            string WorkShop = ((DropDownList)(d.Rows[0].Cells[1].FindControl("DDLWork_Shop"))).SelectedValue;
            TcInfo = tc.GetbyTcodeExis(ToolCode);//, WorkShop
            if (!string.IsNullOrEmpty(TcInfo.Tool_Code))// && !string.IsNullOrEmpty(TcInfo.Work_Shop)
            {
                string str1 = "此代码已存在,请输入新代码！";//在" + WorkShop + "
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
                ((TextBox)(d.Rows[1].Cells[1].Controls[0])).Text = "";
                ((TextBox)(d.Rows[1].Cells[1].Controls[0])).Focus();
                this.Delall.Visible = false;
            }
            else
            {
                if (TCodeCheck(ToolCode, out message) == false)
                {
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + message + "');</script>");
                    ((TextBox)(d.Rows[1].Cells[1].Controls[0])).Text = "";
                    ((TextBox)(d.Rows[1].Cells[1].Controls[0])).Focus();
                    this.Delall.Visible = false;
                }
                else
                {
                    tci.Tool_Code = ((TextBox)(d.Rows[1].Cells[1].Controls[0])).Text.Trim();

                    //string ToolTypeSpec=((TextBox)(d.Rows[2].Cells[1].Controls[0])).Text.Trim();
                    //TcInfo = tc.GetbyToolTypeSpec(ToolTypeSpec,WorkShop);
                    //if (!string.IsNullOrEmpty(TcInfo.Tool_Type_Spec) && !string.IsNullOrEmpty(TcInfo.Work_Shop)) 
                    //{
                    //    string str1 = "此规格在" + WorkShop + "已存在,请输入新规格!";
                    //    ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
                    //    ((TextBox)(d.Rows[2].Cells[1].Controls[0])).Text = "";
                    //    ((TextBox)(d.Rows[2].Cells[1].Controls[0])).Focus();
                    //    return;
                    //}
                    tci.Tool_Type_Spec = ((TextBox)(d.Rows[2].Cells[1].Controls[0])).Text.Trim();
                    tc.Insert(tci);
                    string str = "添加成功！";
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
                    BindTool_Code();
                }
            }

        }


        #region 代码格式验证
        /// <summary>
        /// 代码格式验证
        /// </summary>
        /// <param name="TcodeSN"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool TCodeCheck(string TcodeSN, out string message)
        {
            if (CheckTCodeSnLength(TcodeSN, out message))
            {
                if (CheckBlankTcode(TcodeSN, out message))
                {
                    message = "";
                    return true;
                }

            }
            return false;
        }

        private static bool CheckTCodeSnLength(string TcodeSN, out string message)
        {
            if (TcodeSN.Length == 14)
            {
                message = "";
                return true;
            }
            message = "代码长度有误";
            return false;
        }

        private static bool CheckBlankTcode(string TcodeSn, out string message)
        {
            string SNArray1 = TcodeSn.Substring(1, 1);
            string SNArray2 = TcodeSn.Substring(4, 1);
            string SNArray3 = TcodeSn.Substring(7, 1);
            string SNArray4 = TcodeSn.Substring(10, 1);
            if (SNArray1 == "-" && SNArray2 == "-" && SNArray3 == "-" && SNArray4 == "-")
            {
                message = "";
                return true;
            }
            message = "代码格式有问题,正确格式如:X-XX-XX-XX-XXX";
            return false;
        }
        #endregion

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Delall_Click(object sender, EventArgs e)
        { 
            int intCount = this.GridView1.Rows.Count;
            for (int i = 0; i < intCount; i++)
            {
                CheckBox CheckSingle = this.GridView1.Rows[i].Cells[0].FindControl("CheckSingle") as CheckBox;
                if (CheckSingle.Checked)
                {
                    int ID = int.Parse(GridView1.DataKeys[i].Values[0].ToString());
                    tc.Delete(ID);  
                }
            }
            //删除后的更新
            BindTool_Code();
        }
    }

}
